package com.biglead.admin.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biglead.admin.mapper.PmsProductCategoryAttributeRelationMapper;
import com.biglead.admin.mapper.PmsProductCategoryMapper;
import com.biglead.admin.mapper.ProductMapper;
import com.biglead.admin.mapper.UmsResourceCategoryMapper;
import com.biglead.admin.param.AdminProductCategoryParam;
import com.biglead.admin.pojo.*;
import com.biglead.admin.service.PmsProductCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("productCategoryService")
public class PmsProductCategoryServiceImpl extends ServiceImpl<PmsProductCategoryMapper, PmsProductCategory> implements PmsProductCategoryService {


    @Autowired
    private PmsProductCategoryAttributeRelationMapper pmsProductCategoryAttributeRelationMapper;

    @Autowired
    private  PmsProductCategoryMapper pmsProductCategoryMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public boolean create(AdminProductCategoryParam productCategoryParam) {
        PmsProductCategory productCategory = new PmsProductCategory();
        productCategory.setProductCount(0);
        BeanUtils.copyProperties(productCategoryParam, productCategory);
        //没有父分类时为一级分类
        setCategoryLevel(productCategory);
        //保存新的分类
        this.save(productCategory);
        //创建筛选属性关联
        List<Long> productAttributeIdList = productCategoryParam.getProductAttributeIdList();
        if (!CollectionUtils.isEmpty(productAttributeIdList)) {
            insertRelationList(productCategory.getId(), productAttributeIdList);
        }

        return true;
    }

    @Override
    public boolean update(Long id, AdminProductCategoryParam productCategoryParam) {
        PmsProductCategory productCategory = new PmsProductCategory();
        productCategory.setId(id);
        BeanUtils.copyProperties(productCategoryParam, productCategory);
        setCategoryLevel(productCategory);
        //更新商品分类时要更新商品中的名称
        PmsProduct product = new PmsProduct();
        product.setProductCategoryName(productCategory.getName());

        productMapper.updateProductCategoryName(id, productCategory.getName());


        //同时更新筛选属性的信息
        if (!CollectionUtils.isEmpty(productCategoryParam.getProductAttributeIdList())) {
            //删除原来的
            pmsProductCategoryAttributeRelationMapper.delete(new QueryWrapper<PmsProductCategoryAttributeRelation>().eq("product_category_id", id));
            //新增新的
            insertRelationList(id, productCategoryParam.getProductAttributeIdList());
        } else {


            pmsProductCategoryAttributeRelationMapper.delete(new QueryWrapper<PmsProductCategoryAttributeRelation>().eq("product_category_id", id));
        }
        return this.updateById(productCategory);
    }

    @Override
    public Page<PmsProductCategory> getList(Long parentId, Integer pageSize, Integer pageNum) {
        Page<PmsProductCategory> page = Page.of(pageNum, pageSize);
        //queryWrapper组装查询where条件
        LambdaQueryWrapper<PmsProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件 parentId
        queryWrapper.like(parentId!=null,PmsProductCategory::getParentId, parentId);
        //发起查询
        this.page(page, queryWrapper);
        return page;
    }

    @Override
    public PmsProductCategory getItem(Long id) {
        return this.getById(id);
    }

    @Override
    public boolean delete(Long id) {
        return this.removeById(id);
    }

    @Override
    public boolean updateNavStatus(List<Long> ids, Integer navStatus) {

        UpdateWrapper<PmsProductCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id",ids);
        PmsProductCategory productCategory = new PmsProductCategory();
        productCategory.setNavStatus(navStatus);
        boolean i = this.update(productCategory,updateWrapper);
        return i;
    }

    @Override
    public boolean updateShowStatus(List<Long> ids, Integer showStatus) {
        UpdateWrapper<PmsProductCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id",ids);
        PmsProductCategory productCategory = new PmsProductCategory();
        productCategory.setShowStatus(showStatus);
        boolean i = this.update(productCategory,updateWrapper);
        return i;
    }

    @Override
    public List<PmsProductCategoryWithChildrenItem> listWithChildren() {
        return pmsProductCategoryMapper.listWithChildren();
    }

    /**
     * 批量插入商品分类与筛选属性关系表
     *
     * @param productCategoryId      商品分类id
     * @param productAttributeIdList 相关商品筛选属性id集合
     */
    private void insertRelationList(Long productCategoryId, List<Long> productAttributeIdList) {
        List<PmsProductCategoryAttributeRelation> relationList = new ArrayList<>();
        for (Long productAttrId : productAttributeIdList) {
            PmsProductCategoryAttributeRelation relation = new PmsProductCategoryAttributeRelation();
            relation.setProductAttributeId(productAttrId);
            relation.setProductCategoryId(productCategoryId);
            relationList.add(relation);
        }
        pmsProductCategoryAttributeRelationMapper.insertList(relationList);
    }

    /**
     * 根据分类的parentId设置分类的level
     */
    private void setCategoryLevel(PmsProductCategory productCategory) {
        //没有父分类时为一级分类
        if (productCategory.getParentId() == 0) {
            productCategory.setLevel(0);
        } else {
            //有父分类时选择根据父分类level设置
            PmsProductCategory parentCategory = this.getOne(
                    new QueryWrapper<PmsProductCategory>()
                            .eq("parent_id", productCategory.getParentId()));
            if (parentCategory != null) {
                productCategory.setLevel(parentCategory.getLevel() + 1);
            } else {
                productCategory.setLevel(0);
            }
        }
    }
}
